<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="冷库" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
<!--
              <j-dict-select-tag type="list" v-model="queryParam.storehouse" :trigger-change="true" dictCode="flower_storehouse,name,code" placeholder="请选择冷库"/>
-->
              <a-select  v-model="queryParam.storehouse"    placeholder='请选择冷库'>
                <a-select-option v-for='d in hoseList' :key='d.id' :value='d.code'>{{ d.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="产品">
              <a-select v-model='queryParam.variety' placeholder="请选择产品"  >
                <a-select-option v-for="(d,i) in varietyList"  :key="d.title" :value="d.text">{{ d.value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label='打包规格'>
                <a-select   v-model='queryParam.packingSpecifications' placeholder="请选择打包规格"  >
                  <a-select-option v-for="(d,i) in psfList"  :key="d.title" :value="d.text">{{ d.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label='单位'>
                <a-select v-model='queryParam.flowerPoleLength' placeholder="请选择单位"  >
                  <a-select-option v-for="(d,i) in lengthList"  :key="d.id" :value="d.title">{{ d.title }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('产品货损')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-popconfirm title='确定确认吗?' @confirm='() => loseData(record)' v-if='record.state === 0'>
            <a-button type='primary' style='margin-right: 5px'>确认报损</a-button>
          </a-popconfirm>
          <a v-if="record.state === 0" @click="handleEdit(record)">编辑</a>
          <a v-if="record.state === 1" @click="getSubIte(record)">详情</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
<!--               <a-menu-item v-if="record.state === 0">-->
<!--                <a-popconfirm title="确定删除吗?" @confirm="() => loseData(record)">-->
<!--                  <a>确认</a>-->
<!--                </a-popconfirm>-->
<!--              </a-menu-item>-->
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <flowerLose-modal ref="modalForm" @ok="modalFormOk"></flowerLose-modal>

    <a-modal v-model:visible="preSaleModel" title="预售库损信息" @ok="hidePreSaleModel" @cancel="hidePreSaleModel" width="1200px">
      <PreSaleFlowerLose :flower-lose-id="currentFlowerLoseId" v-if="preSaleModel"></PreSaleFlowerLose>
    </a-modal>

    <a-modal v-model:visible="stockModel" title="冷库库损信息" @ok="hideFlowerTagModel" @cancel="hideFlowerTagModel" width="1200px">
      <FlowerTagLose :flower-lose-id="currentFlowerLoseId"  v-if="stockModel"></FlowerTagLose>
    </a-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import FlowerLoseModal from './modules/FlowerLoseModal'
  import {postAction,getAction} from '../../api/manage'
  import PreSaleFlowerLose from './PreSaleFlowerLose'
  import FlowerTagLose from './FlowerTagLose'
  import { ajaxGetDictItems ,getFlowerType} from '@/api/api'
  export default {
    name: "FlowerLoseList",
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      FlowerLoseModal,
      PreSaleFlowerLose,
      FlowerTagLose
    },
    data () {
      return {
        hoseList: [], //冷库
        description: '产品货损管理页面',
        preSaleModel:false,
        stockModel: false,
        currentFlowerLoseId: '',
        // 表头
        columns: [
          {
            title:'冷库',
            align:"center",
            dataIndex: 'storehouse_dictText'
          },  {
            title:'采收产品',
            align:"center",
            dataIndex: 'variety'
          },
          {
            title:'产品编码',
            align:"center",
            dataIndex: 'varietyCode'
          },
          {
            title:'打包规格',
            align:"center",
            dataIndex: 'packingSpecifications'
          },

          {
            title:'单位',
            align:"center",
            dataIndex: 'flowerPoleLength'
          },
          {
            title:'库损数量',
            align:"center",
            dataIndex: 'loseNum'
          },
          {
            title:'库损类型',
            align:"center",
            dataIndex: 'loseType'
          },
          // {
          //   title:'采收分类',
          //   align:"center",
          //   dataIndex: 'classify'
          // },
          {
            title: '状态',
            align:"center",
            dataIndex: 'state',
            customRender:function (text) {
              if(text === 0){
                return "待确认"
              }
              if(text === 1){
                return "已确认"
              }
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            // fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/flower/flowerLose/list",
          delete: "/flower/flowerLose/delete",
          deleteBatch: "/flower/flowerLose/deleteBatch",
          exportXlsUrl: "/flower/flowerLose/exportXls",
          importExcelUrl: "/flower/flowerLose/importExcel",
          flowerLoseUrl: "/flower/flowerLose/flowerLose",
          getFlowerTypeUrl: "/flower/flowerVariety/getFlowerType",
          getPackingSpecificationsUrl: "/flower/flowerVariety/getPackingSpecifications"
        },
        dictOptions:{},
        psfList: [], //打包规格
        lengthList: [], //长度信息
        varietyList: [],
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    created() {
      this.initpsfDict()//初始化打包规格
      this.initlengthDict()//长度规格
      this.initVarietyDict()//鲜花产品
      this.hoseList =  JSON.parse( localStorage.getItem("ch_storehouse")?localStorage.getItem("ch_storehouse"):"")
    },
    methods: {
      hidePreSaleModel(){
        this.preSaleModel = false;
      },
      hideFlowerTagModel(){
        this.stockModel = false;
      },
      initDictConfig(){
      },
      getSubIte(record){
        this.currentFlowerLoseId = record.id
        if(record.loseType === '预售货损'){
          this.preSaleModel = true
        }
        if(record.loseType === '库存货损'){
          this.stockModel = true
        }
      },
      loseData(record){
        postAction(this.url.flowerLoseUrl, record).then((res) => {
          if (res.success) {
            record.state = 1
            this.$message.success(res.message)
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },

      //初始化打包规格信息
      initpsfDict() {

        getAction(this.url.getPackingSpecificationsUrl).then((res) => {
          if (res.success) {
            this.psfList = res.result
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      }, //
      initlengthDict() {
        const dictCode = 'planting_flower_pole'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.lengthList = res.result
          }
        })
      },
      initVarietyDict(){
        getAction(this.url.getFlowerTypeUrl).then((res) => {
          if (res.success) {
            this.varietyList = res.result
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>